package com.ttt.adbgui.function;

import com.ttt.adbgui.jcomp.JFrameComponent;
import com.ttt.adbgui.utils.JavaCMD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Component
@Slf4j
public class Dialog {

    @Autowired
    private JFrameComponent component;
    @Autowired
    private JavaCMD javaCMD;
    @Autowired
    private Environment env;

    /*
    * 文件表格对文件进行操作的对话框
    * 对单个按键单独添加事件
    * result 关闭后返回-1
    * */
    public void dialogFileTable(JPanel panel){

        JButton[] bs={new JButton("下载"),new JButton("复制"),new JButton("查看")};
        bs[0].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                log.info("点击后将下载当前文件夹或者文件到本地");
                JTable tableFile = component.getTableFile();
                int selectedRow = tableFile.getSelectedRow();
                int selectedColumn = tableFile.getSelectedColumn();
                String valueAt = String.valueOf(tableFile.getValueAt(selectedRow, selectedColumn));
                valueAt = valueAt.substring(1,valueAt.length());
                String fileParentPath = component.getFileParentPath();
                String cmd = "cmd /c adb pull " + fileParentPath + " /" + valueAt + " " + env.getProperty("saveFilePath");
                log.info(cmd);
                javaCMD.execSubmit(cmd);
            }
        });
        JOptionPane.showOptionDialog(panel, "请选择其中的一项：", "选择", 1, 3, null, bs, bs[0]);
    }
}
